var lmIndex=0;
var _rxcxChkN02=false;
var _rxcxChkN = 1;
var _maxPoun = 10;
var _type=['选二任选','选二连组','选二连直','选三任选','选三前组','选三前直','选四任选','选五任选'];
function RefRate()
{
	$.getJSON("op.php?op=xml_3d&fp=rate_5h&playtype="+playtype+"&lotto_id_5h="+lotto_id+"&gametype="+gametype+"&disk="+$("#select_disk").val()+"&bid="+(lmIndex+10),null,function(r){
		for(i=0;i<8;i++)
		{
			$("#Max_"+(i+10)).text(r.mbBet[i].ratesS)
			$("#Min_"+(i+10)).text(r.mbBet[i].ratesM)
		}
		if(lmIndex==2)
		{
			show_tr1(_type[lmIndex],r["b"+(lmIndex+10)].rates,['前<br>位','后<br>位']);
		}
		else if(lmIndex==5)
		{
			show_tr1(_type[lmIndex],r["b"+(lmIndex+10)].rates,['前<br>位','中<br>位','后<br>位']);		
		}
		else
		{
			show_num(_type[lmIndex],r["b"+(lmIndex+10)].rates);		
		}
		window.setTimeout("ClearColor()",1000);
	});	
}
$(document).ready(function(e) {
	RefRate();
    $("#quick02_a_0 input").bind("click",function(b){
			var arr=new Array();
			var v=playtype.substr(2,3);
			switch($(this).val())
			{
				case '单':
					arr=['01','05','09','13','17','03','07','11','15','19'];
					break;
				case '双':
					arr=['02','04','06','08','10','12','14','16','18','20'];
					break;
				case '大':
					arr=['11','12','13','14','15','16','17','18','19','20'];
					break;
				case '小':
					arr=['01','02','03','04','05','06','07','08','09','10'];
					break;
				case '合单':
					arr=['01','05','09','10','14','18','03','07','12','16'];
					break;
				case '合双':
					arr=['13','17','02','06','11','15','19','04','08','20'];
					break;	
			}
			$('#tblMy3DArea input:checked').attr("checked",false);
			$('#tblMy3DArea input:disabled').attr("disabled",false);
			for(k=0;k<arr.length;k++)
			{
				$("#b_0_"+arr[k]).attr("checked",true);	
			}
			computeUnion();
		});
	$("#quick02_a_1 input").bind("click",function(b){
		var arr=new Array();
		var v=playtype.substr(2,3);
		switch($(this).val())
		{
			case " 0 ":
				arr=['01','02','03','04','05','06','07','08','09'];
				break;	
			case " 1 ":
				arr=['10','11','12','13','14','15','16','17','18','19'];
				break;
			case " 2 ":
				arr=['20'];
				break;
		}
		$('#tblMy3DArea input:checked').attr("checked",false);
		$('#tblMy3DArea input:disabled').attr("disabled",false);
		for(k=0;k<arr.length;k++)
			$("#b_0_"+arr[k]).attr("checked",true);		
		computeUnion();
	});
	$("#quick02_a_2 input").bind("click",function(b){
		var v=$(this).val();
		var v2=playtype.substr(2,3);
		$('#tblMy3DArea input:checked').attr("checked",false);
		$('#tblMy3DArea input:disabled').attr("disabled",false);
		$("input[name^='b_0_']").each(function(index, element) {
            var s=element.id.split("_");
			if(parseInt(s[s.length-1].substr(1,1))==parseInt(v))
			{
				element.checked=true;	
			}
        });	
		computeUnion();
	});
	$("#quick02_a_3 input").bind("click",function(b){
		var arr=new Array();
		var v=playtype.substr(2,3);
		switch($(this).val())
		{
			case "中":
				arr=['01','02','03','04','05','06','07'];
				break;	
			case "发":
				arr=['08','09','10','11','12','13','14'];
				break;
			case "白":
				arr=['15','16','17','18','19','20'];
				break;
		}
		$('#tblMy3DArea input:checked').attr("checked",false);
		$('#tblMy3DArea input:disabled').attr("disabled",false);
		for(k=0;k<arr.length;k++)
			$("#b_0_"+arr[k]).attr("checked",true);	
		computeUnion();	
	});
	$("#quick02_a_4 input").bind("click",function(b){
		var arr=new Array();
		var v=playtype.substr(2,3);
		switch($(this).val())
		{
			case "东":
				arr=['01','05','09','13','17'];
				break;	
			case "南":
				arr=['02','06','10','14','18'];
				break;
			case "西":
				arr=['03','07','11','15','19'];
				break;
			case "北":
				arr=['04','08','12','16','20'];
				break;
		}
		$('#tblMy3DArea input:checked').attr("checked",false);
		$('#tblMy3DArea input:disabled').attr("disabled",false);
		for(k=0;k<arr.length;k++)
			$("#b_0_"+arr[k]).attr("checked",true);	
		computeUnion();
	});
	$("input[name='rcbettype']").bind("click",function(b){
		var index=0;
		index=$(this).parent().index();
		var tr=$(this).parent().parent();
		tr.find("td").get(lmIndex).style.backgroundColor="#eeeeee";
		tr.next().find("td").get(lmIndex).style.backgroundColor="";
		tr.find("td").get(index).style.backgroundColor="#FFFF99";
		tr.next().find("td").get(index).style.backgroundColor="#FFFFCC";
		lmIndex=index;
		$("#quick5hTypeSpana").show();
		switch ($(this).val()) {
			case '10':
				_rxcxChkN = 1;
				_maxPoun = 10;
				break;
			case '11':
				_rxcxChkN = 1;
				_maxPoun = 10;
				break;
			case '12':
				_rxcxChkN = 1;
				_maxPoun = 20;
				$("#quick5hTypeSpana").hide();
				break;
			case '13':
				_rxcxChkN = 2;
				_maxPoun = 10;
				break;
			case '14':
				_rxcxChkN = 2;
				_maxPoun = 10;
				break;
			case '15':
				_rxcxChkN = 2;
				_maxPoun = 20;
				$("#quick5hTypeSpana").hide();
				break;
			case '16':
				_rxcxChkN = 3;
				_maxPoun = 8;
				break;
			case '17':
				_rxcxChkN = 4;
				_maxPoun = 8;
				break;
			default:
				return '-rncn error';
				break;
		}
		$('#hmPanel').empty();
		$('#dtPanel').empty();
		RefRate();	
	});	
	$("#btnBet").bind("click",function(b){
		$("#B5hTypeMon").val($("#changeMoney").val());
		if(lmIndex==2)
		{
			$("#B5hTypeSpan01").val($("#B5hTypeSpan030Span").text());
			$("#B5hTypeSpan02").val($("#B5hTypeSpan031Span").text());		
		}
		else if(lmIndex==5)
		{
			$("#B5hTypeSpan01").val($("#B5hTypeSpan030Span").text());
			$("#B5hTypeSpan02").val($("#B5hTypeSpan031Span").text());		
			$("#B5hTypeSpan03").val($("#B5hTypeSpan032Span").text());	
		}
		else
			$("#B5hTypeSpan01").val($("#B5hTypeSpan01Span").text());
	});
	$("#tblMy3DArea input[type=checkbox]").live("click",function(b){
		if(lmIndex==2 || lmIndex==5)
			computeUnionS();
		else
			computeUnion();
	});
	$("input[name=quick5h]").bind("click",function(b){
		$('#tblMy3DArea input:checked').attr("checked",false);
		$('#tblMy3DArea input:disabled').attr("disabled",false);
		$('#hmPanel').empty();
		$('#dtPanel').empty();
		_rxcxChkN02=($(this).val()=="b");	
	});
	$("#reset").bind("click",function(b){
		$('#hmPanel').empty();
		$('#dtPanel').empty();
		$('#tblMy3DArea input:checked').attr("checked",false);
		$('#tblMy3DArea input:disabled').attr("disabled",false);	
	});
});
function computeUnion() {
    $('#dtSpan').empty();
    if (_rxcxChkN > -1) {
        var tempSValArr = [];
        var dobuledNum = 0;
        if (_rxcxChkN02) { // 胆
            if ($('#tblMy3DArea input:disabled:checked').length < _rxcxChkN) {
                $('#tblMy3DArea input:checked').attr('disabled', 'disabled');
                $('#tblMy3DArea input:checked:disabled').each(function(idx, oVal) {
                    tempSValArr.push($(oVal).val());
                    return true;
                });
                $('#hmPanel').html('[<span id="B5hTypeSpan01Span">' + tempSValArr.join(",") + '</span>]<br />');
            } else {
                var temp01 = '';
                var temp02 = '';
                $('#tblMy3DArea input:disabled:checked').each(function(idx, oVal) {
                    tempSValArr.push($(oVal).val());
                    return true;
                });
                temp01 = tempSValArr.join(",");
                tempSValArr = [];
                dobuledNum = 0;
                $('#tblMy3DArea input:checked:not(:disabled)').each(function(idx, oVal) {
                    tempSValArr.push($(oVal).val());
                    dobuledNum++;
                    return true;
                });
                temp02 = tempSValArr.join(",");
                $('#hmPanel').html('[<span id="B5hTypeSpan01Span">' + temp01 + '</span>][<span id="B5hTypeSpan02Span">' + temp02 + '</span>] 共 ' + dobuledNum + ' 组<br />');
            }
        } else { // 碰
            // 限制
            if ($('#tblMy3DArea input:checked').length == _maxPoun) {
                $('#tblMy3DArea input:not(:checked)').attr("disabled", "disabled");
            } else {
                $('#tblMy3DArea input').removeAttr("disabled");
            }
            $('#tblMy3DArea input:checked').each(function(idx, oVal) {
                tempSValArr.push($(oVal).val());
                dobuledNum++;
                return true;
            });
            if (dobuledNum < _rxcxChkN) {
                $('#hmPanel').html('[<span id="B5hTypeSpan01Span">' + tempSValArr.join(",") + '</span>]共 0 组<br />');
            } else {
                var betUnionLength = tempSValArr.length;
                //组合数 
                switch (_rxcxChkN) {
                    case 0:
                        var bet_Length = betUnionLength;
                        break;
                    case 1:
                        var bet_Length = betUnionLength * (betUnionLength - 1) / 2;
                        break;
                    case 2:
                        var bet_Length = betUnionLength * (betUnionLength - 1) * (betUnionLength - 2) / 6;
                        break;
                    case 3:
                        var bet_Length = betUnionLength * (betUnionLength - 1) * (betUnionLength - 2) * (betUnionLength - 3) / 24;
                        break;
                    case 4:
                        var bet_Length = betUnionLength * (betUnionLength - 1) * (betUnionLength - 2) * (betUnionLength - 3) * (betUnionLength - 4) / 120;
                        break;
                    default:
                        alert('error rxcxChkN');
                        return false;
                        break;
                }

                $('#hmPanel').html('[<span id="B5hTypeSpan01Span">' + tempSValArr.join(",") + '</span>]共 ' + bet_Length + ' 组<br />');
            }
        }
    }
}
function computeUnionS() {
    $('#hmPanel').empty();
    if (_rxcxChkN > -1) {
        var tbdNum = (lmIndex==2?2:3);
        var sendDataArr = [];
        var i = 0;
        var okL = 0;
        var showStr = '';
        var bet_Length = 1;
        // tblMy3DArea check for enabled numbers
        if ($('#tblMy3DArea input:checked').length == _maxPoun) {
            $('#tblMy3DArea input:not(:checked)').attr("disabled", "disabled");
        }
        else {
            $('#tblMy3DArea input').removeAttr("disabled");
        }

        for (i = 0; i < tbdNum; i++) {
            sendDataArr[i] = [];
            $('#tbdData_' + i + ' input:checked').each(function(idx, oVal) {
                sendDataArr[i].push($(oVal).val());
                return true;
            });
            if (sendDataArr[i].length > 0) {
                okL++;
            }
            if (i) {
                showStr += ' X ';
            }
            showStr += '[<span id="B5hTypeSpan03' + i + 'Span">' + sendDataArr[i].join(',') + '</span>]';
        }

        if (okL == tbdNum) {
            bet_Length = 0;
            if (okL == 2) {
                var j = 0;
                for (i = 0; i < sendDataArr[0].length; i++) {
                    for (j = 0; j < sendDataArr[1].length; j++) {
                        if (sendDataArr[0][i] != sendDataArr[1][j]) {
                            bet_Length++;
                        }
                    }
                }
            } else {
                var j = 0;
                var k = 0;
                for (i = 0; i < sendDataArr[0].length; i++) {
                    for (j = 0; j < sendDataArr[1].length; j++) {
                        if (sendDataArr[0][i] == sendDataArr[1][j]) {
                            continue;
                        }
                        for (k = 0; k < sendDataArr[2].length; k++) {
                            if (sendDataArr[0][i] == sendDataArr[2][k]) {
                                continue;
                            }
                            if (sendDataArr[1][j] == sendDataArr[2][k]) {
                                continue;
                            }
                            bet_Length++;
                        }
                    }
                }
            }
            $('#hmPanel').html(showStr + '共 ' + bet_Length + ' 组<br />');
        }
        else {
            $('#hmPanel').html(showStr + '共 0 组<br />');
        }
    }
}
function show_num(t,r)
{
	var html='<table width="745" border="0" id="tblMy3DArea" cellspacing="0" cellpadding="0" class="fist">';
	html+='<tr align="center" style=" background-color:#f1efef;">'+
          '  <td height="25" colspan="15" class="biankuang" style="font-size:16px; color:#0000FF; ">'+t+'</td>'+
          '</tr><tr>'+
		  '  <Td valign="top">'+show_tr_item(['01','02','03','04'],[r[0],r[1],r[2],r[3]],'')+'</td>'+
		  '  <Td valign="top">'+show_tr_item(['05','06','07','08'],[r[4],r[5],r[6],r[7]],'')+'</td>'+
		  '  <Td valign="top">'+show_tr_item(['09','10','11','12'],[r[8],r[9],r[10],r[11]],'')+'</td>'+
		  '  <Td valign="top">'+show_tr_item(['13','14','15','16'],[r[12],r[13],r[14],r[15]],'')+'</td>'+
		  '  <Td valign="top">'+show_tr_item(['17','18','19','20'],[r[16],r[17],r[18],r[19]],'')+'</td>'+
		  '</tr></table>';
	$("#xzPanel").html(html);
}
function show_tr1(t,r,arr)
{
	var html='<table width="745" border="0" id="tblMy3DArea" cellspacing="0" cellpadding="0" class="fist">';
	html+='<tr align="center" style=" background-color:#f1efef;">'+
          '  <td height="25" colspan="15" class="biankuang" style="font-size:16px; color:#0000FF; ">'+t+'</td>'+
          '</tr><tbody>';
	for(k=0;k<arr.length;k++)
	{
		html+='<tr id="tbdData_'+k+'">'+
          '  <Td width="30" align="center" class="biankuang">'+arr[k]+'</td>'+
          '  <Td valign="top">'+show_tr_item(['01','02','03'],[r[0],r[1],r[2]],'',true)+'</td>'+
		  '  <Td valign="top">'+show_tr_item(['04','05','06'],[r[3],r[4],r[5]],'',true)+'</td>'+
		  '  <Td valign="top">'+show_tr_item(['07','08','09'],[r[6],r[7],r[8]],'',true)+'</td>'+
		  '  <Td valign="top">'+show_tr_item(['10','11','12'],[r[9],r[10],r[11]],'',true)+'</td>'+
		  '  <Td valign="top">'+show_tr_item(['13','14','15'],[r[12],r[13],r[14]],'',true)+'</td>'+
		  '  <Td valign="top">'+show_tr_item(['16','17','18'],[r[15],r[16],r[17]],'',true)+'</td>'+
		  '  <Td valign="top">'+show_tr_item(['19','20',''],[r[18],r[19],0],(k==0?'<input type="button" class="colorBtnGray2" style="width:50px;margin-top:5px" name="bcBtn" value="包牌">':''),true)+'</td>'+
		'</tr>';
	}
	html+='</tbody></table>';
	$("#xzPanel").html(html);
}

function show_tr_item(arr,rate,str,flag)
{
	if(flag)
	{
		var html='<table border="0" style="width:100%">'+
			'<tr align="center" style="background-color:#f1efef;">'+
			'  <td height="25" class="biankuang01" nowrap style="font-size:12px;width:40px!important">号码</td>'+
			'  <td class="biankuang01" nowrap style="font-size:12px;width:30px!important">赔率</td>'+
			'  <td class="biankuang01" nowrap style="font-size:12px;width:30px!important">选择</td>'+
			'</tr>';
	}
	else{
		var html='<table border="0" style="width:100%">'+
			'<tr align="center" style="background-color:#f1efef;">'+
			'  <td height="25" class="biankuang01" nowrap style="font-size:12px;width:40px!important">号码</td>'+
			'  <td class="biankuang01" nowrap style="font-size:12px;width:60px!important">赔率</td>'+
			'  <td class="biankuang01" nowrap style="font-size:12px;width:40px!important">选择</td>'+
			'</tr>';
	}
	for(i=0;i<arr.length;i++)
	{
		num=arr[i];
		var c=(gametype==23?'  <td height="28" class="cent_ncnumber cent_ncnum'+num+'"></td>':'  <td height="28" class="ball_'+(parseInt(num)<=18?"yellow":"red01")+'">'+num+'</td>');
		if(num!=""){
			html+='<tr align="center">'+c+
			'  <td class="biankuang"><span class="rate" id="rb_0_'+num+'">'+rate[i]+'</span></td>'+
			'  <td class="biankuang"><input name="b_0_'+num+'" id="b_0_'+num+'" type="checkbox" value="'+num+'" maxlength="5"></td>'+
			'</tr>';
		}
		else
		{
			html+='<tr align="center">'+
			'  <td height="33" class="biankuang" colspan=3>'+str+'</td>'+
			'</tr>';
	
		}
	}
    html+='</table>';	
	return html;
}
